/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.webhomesites.configuracao.infra;

/**
 *
 * @author Webhome
 */
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.hibernate.Session;
import org.hibernate.ejb.HibernateEntityManager;
  
/** 
* 
* @author Tomaz Lavieri 
*/  
public class EntityUtil {  
    private static EntityManagerFactory entityManagerFactory;  
    private static EntityManager entityManager;  
  
    public static EntityManager getPublicEntityManager() {  
        if (entityManager == null)  
            entityManager = java.beans.Beans.isDesignTime() ?  
                null :  
                getEntityManagerFactory().createEntityManager();  
  
        return entityManager;  
    }  
  
    public static EntityManager getNewEntityManager() {  
        return getEntityManagerFactory().createEntityManager();  
    }  
  
    public static synchronized EntityManagerFactory getEntityManagerFactory() {  
        if (entityManagerFactory == null) {  
            entityManagerFactory = java.beans.Beans.isDesignTime() ?  
                null :  
                javax.persistence.Persistence  
                    .createEntityManagerFactory("HibernateJPAPU");  
        }  
        return entityManagerFactory;  
    }  
      
    public static Session getPublicSession() {  
        return ((HibernateEntityManager)getPublicEntityManager()).getSession();  
    }  
      
    public static Session getNewSession() {  
        return ((HibernateEntityManager)getNewEntityManager()).getSession();  
    }  
}  